/**
 * Created on Apr 23, 2007
 */
package x.java.net.protocol.v;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.net.URL;
import java.security.Permission;

import jvs.vfs.FileSystem;
import jvs.vfs.IFile;

/**
 * @author qiangli
 * 
 */
public class URLConnection extends java.net.URLConnection {
	private FileSystem fs = FileSystem.getFileSystem();

	protected IFile file = null;
	protected URI uri = null;
	
	/**
	 * @param url
	 */
	public URLConnection(URL url) {
		super(url);
		try {
			uri = new URI(url.getPath());
		} catch (Exception e) {
			//debug(e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.net.URLConnection#connect()
	 */
	public void connect() throws IOException {
		//
	}
	
	/**
	 * lazy initialization
	 */
	synchronized private IFile getIFile() {
		if (file == null) {
			try {
				//file = fs.createIFile(new File(uri));
				file = fs.createIFile(uri.getPath());
				//trace("URLConntection ifile: "+file+" url: "+url);
			} catch (Exception e) {
				//debug(e);
				e.printStackTrace();
			} 
		}
		return file;
	}

	public Object getContent() throws IOException {
		if (getIFile().canRead() && getIFile().isFile()) {
			return getIFile().getContent();
		}
		return null;
	}

	public String getContentEncoding() {
		if (getIFile().canRead()) {
			return getIFile().getContentEncoding();
		}
		return null;
	}

	public int getContentLength() {
		if (getIFile().canRead() && getIFile().isFile()) {
			return (int)getIFile().getLength();
		} 
		return -1;
	}

	public String getContentType() {
		if (getIFile().canRead()) {
			return getIFile().getContentType();
		}
		return null;
	}

	public InputStream getInputStream() throws IOException {
		try {
			if (getIFile().canRead() && getIFile().isFile()) {
				InputStream is = getIFile().getInputStream();
				return is;
			}
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	public Permission getPermission() {
		return null;
	}
	
	public void setPermission(Permission perm) {
	}

	public OutputStream getOutputStream() throws IOException {
		if (getIFile().canWrite() && getIFile().isFile()) {
			return getIFile().getOutputStream();
		}
		return null;
	}

	public void setContent(String content) throws IOException {
		if (getIFile().canWrite() && getIFile().isFile()) {
			getIFile().setContent(content);
		}
	}

	public void setContentEncoding(String encoding) {
		if (getIFile().canWrite()) {
			getIFile().setContentEncoding(encoding);
		}
	}

	public void setContentLength(int len) {
		if (getIFile().canWrite() && getIFile().isFile()) {
			getIFile().setLength(len);
		}
	}

	public void setContentType(String type) {
		if (getIFile().canWrite()) {
			getIFile().setContentType(type);
		}
	}

	/*
	 * 
	 */
//	private void debug(Object o) {
//		if (o instanceof Throwable) {
//			Log.log(Log.TRACE,this,o);
//			return;
//		}
//		Log.log(Log.DEBUG,this,o);
//	}
	
//	private void trace(Object o) {
//		if (o instanceof Throwable) {
//			Log.log(Log.TRACE,this,o);
//			return;
//		}
//		Log.log(Log.TRACE,this,o);
//	}
}
